# 定义编译器和编译选项, make 001 方式构建并运行 程序
PREFIX :=
CC = gcc
CXX = g++
CFLAGS = -Wall -Wextra
CXXFLAGS = -Wall -Wextra -std=c++11

BUILD_DIR = build
$(shell mkdir -p $(BUILD_DIR))
.PHONY:  

# 如果 .cpp 不存在，尝试匹配 .c 文件
%: %.c
	@echo "compile  $^..."
	@$(CC) $(CFLAGS) -o $@ $<
	@echo " "
	@echo "Running $@..."
	@./$@

# 优先匹配 .cpp 文件（如果存在）
%: %.cpp
	@echo "compile  $^..."
	@$(CXX) $(CXXFLAGS) -o $(BUILD_DIR)/$@ $<
	@echo "Running $@..."
	@./$(BUILD_DIR)/$@


# 处理无源文件的情况
.DEFAULT:
	@echo "Error: No source file found for target '$*'. Create '$*.c' or '$*.cpp'."
	@exit 1

# 新增 clean 目标，用于删除 build 目录及其内容
# clean:
# 	@echo "Cleaning up..."
# 	@rm -rf $(BUILD_DIR)
# 	@echo "Cleaning completed."